//
//  ICTabBarController.swift
//  whale
//
//  Created by topnet on 2022/7/20.
//

import UIKit
import Lottie
import RxSwift
class ICTabBarController: UITabBarController {

    fileprivate var adsModel: AdsModel = .init()
    fileprivate var dispose: DisposeBag = .init()

    
    var currentIndex: Int? {
        
        set {
            if newValue != nil {
                selectedIndex = newValue!
            }
        }
        
        get {
            selectedIndex
        }
    }
    
    
    
    override func loadView() {
        super.loadView()
        
        let vc = NetVC()
        let nav = UINavigationController(rootViewController: vc)
        nav.tabBarItem = UITabBarItem(title: "首页", image: UIImage(named: "tab_net_n"), selectedImage: UIImage(named: "tab_net_y")?.withRenderingMode(.alwaysOriginal))
        nav.tabBarItem.tag = 0
        
        let vc1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeView")
        
        let nav1 = UINavigationController(rootViewController: vc1)
        nav1.tabBarItem = UITabBarItem(title: "vpn", image: UIImage(named: "ic_tabbar_home"), selectedImage: UIImage(named: "ic_tabbar_home_selected")?.withRenderingMode(.alwaysOriginal))
        nav1.tabBarItem.tag = 1
        nav1.isNavigationBarHidden = true
        nav1.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.content], for: .normal)
        
//        let vc2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PremiumView")
//        let nav2 = UINavigationController(rootViewController: vc2)
//        nav2.tabBarItem = UITabBarItem(title: "订阅", image: UIImage(named: "ic_tabbar_crow"), selectedImage: UIImage(named: "ic_tabbar_crow_selected")?.withRenderingMode(.alwaysOriginal))
//        nav2.tabBarItem.tag = 1
//        nav2.isNavigationBarHidden = true
//        nav2.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.content], for: .normal)
        
        let vc3 = DeviceVC()
        let nav3 = UINavigationController(rootViewController: vc3)
        nav3.tabBarItem = UITabBarItem(title: "设备", image: UIImage(named: "ic_tabbar_device"), selectedImage: UIImage(named: "ic_tabbar_device_selected")?.withRenderingMode(.alwaysOriginal))
        nav3.tabBarItem.tag = 2
        
        let vc4 = MeVC()
        let nav4 = UINavigationController(rootViewController: vc4)
        nav4.tabBarItem = UITabBarItem(title: "我的", image: UIImage(named: "ic_tabbar_profile"), selectedImage: UIImage(named: "ic_tabbar_profile_selected")?.withRenderingMode(.alwaysOriginal))
        nav4.tabBarItem.tag = 3
        nav4.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.content], for: .normal)
        
        tabBar.tintColor = ._blue
        viewControllers = [nav, nav1, nav3, nav4]
        tabBar.isOpaque = false
        tabBar.backgroundColor = .bg
               
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupData()
    }
    
    func setupData() {
        self.adsModel = .init()
        self.adsModel.ads.subscribe { ads in
            WitWork.share.ads = ads
            guard let viewController = self.selectedViewController  else {return}
            self.loadAd(viewController: viewController)
        } onCompleted: {
        }.disposed(by: self.dispose)
        self.adsModel.getAds()
    }
    
    func loadAd(viewController: UIViewController) {
        var baseView: BaseView?
        if viewController.isMember(of: BaseView.self) {
            baseView = viewController as? BaseView
        }
        if viewController.isMember(of: NavigationViewController.self) {
            baseView = (viewController as? NavigationViewController)?.viewControllers.first as? BaseView
        }
        baseView?.loadAd()
    }

    override var shouldAutorotate: Bool {
        return true
    }
    
    private var animationView: AnimationView?

}

extension ICTabBarController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController is ProfileView {
            guard let email = WitWork.share.user?.email,
                  email.validateEmailString() == true else {
                let authenVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AuthenNavigation") as! NavigationViewController
                authenVC.modalPresentationStyle = .formSheet
                if let loginVC = authenVC.viewControllers.first as? LoginView {
                    loginVC.completion = { finished in
                        tabBarController.selectedIndex = 2
                        self.dismiss(animated: true, completion: nil)
                    }
                }
                self.present(authenVC, animated: true, completion: nil)
                return false
            }
        }
        return true
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        self.loadAd(viewController: viewController)
    }

}


